﻿namespace ManagerClinic.CanLamSang
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Linq;
    using System.Windows.Forms;
    using DevExpress.XtraEditors;
    using System.Configuration;
    using ManagerClinic.Ultilites.HtmlEditor.Buttons;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Modules;
    using ManagerClinic.Ultilities.Ultility;

    public partial class frmPhieuSieuAm : DevExpress.XtraEditors.XtraForm
    {

        string MaPhieuSieuAm = string.Empty, MaHoSoKhamBenh = string.Empty, MaDichVu = string.Empty;
        Int32 Status = 0;
        bool resultInsert = false;
        public event EventHandler _SavePhieuCanLamSang = null;

        PhieuSieuAmBussiness phieuSieuAmBussiness = new PhieuSieuAmBussiness();
        PhieuSieuAmChiTietBussiness phieuSieuAmChiTietBussiness = new PhieuSieuAmChiTietBussiness();
        ThuPhiDichVuBusiness thuPhiDichVuBusiness = new ThuPhiDichVuBusiness();
        DmBacSyBusiness dmBacSyBusiness = new DmBacSyBusiness();
        HoSoKhamBenhNgoaiTruBusiness hoSoKhamBenhNgoaiTruBusiness = new HoSoKhamBenhNgoaiTruBusiness();

        PhieuSieuAmEntity phieuSieuAmEntity = null;
        HoSoKhamBenhNgoaiTruEntity hoSoKhamBenhNgoaiTruEntity = null;

        public frmPhieuSieuAm(Int32 status, string maDichVu, HoSoKhamBenhNgoaiTruEntity obj)
        {
            InitializeComponent();

            PredefinedButtonSets.SetupDefaultButtons(this.txtKetQua);
            PredefinedButtonSets.SetupDefaultButtons(this.txtKetLuan);

            Status = status;
            hoSoKhamBenhNgoaiTruEntity = obj;
            MaDichVu = maDichVu;
            MaPhieuSieuAm = ConstantManagerClinic.setCodeManagerClinic(ConstantManagerClinic.maPhieuSA);

            ucThongTinHanhChinh.LoadThongTinHanhChinh(hoSoKhamBenhNgoaiTruEntity.MaBenhNhan);
            ucChiDinhKhoaPhong.LoadDmKhoaKham();
            ucChiDinhKhoaPhong.LoadDmKhoaCanLamSang();
            ucChiDinhKhoaPhong.LoadDmPhongKham();
            ucChiDinhKhoaPhong.LoadDmPhongCanLamSang();
            ucChiDinhKhoaPhong.LoadDmDichVuChiTiet(MaDichVu);

        }

        public frmPhieuSieuAm(Int32 status, string maPhieuSieuAm)
        {
            InitializeComponent();
            PredefinedButtonSets.SetupDefaultButtons(this.txtKetQua);
            PredefinedButtonSets.SetupDefaultButtons(this.txtKetLuan);

            Status = status;
            MaPhieuSieuAm = maPhieuSieuAm;

            phieuSieuAmEntity = phieuSieuAmBussiness.GetPhieuSieuAm(MaPhieuSieuAm);
            hoSoKhamBenhNgoaiTruEntity = hoSoKhamBenhNgoaiTruBusiness.GetHoSoKhamBenhNgoaiTru(phieuSieuAmEntity.MaHoSo);
            ucThongTinHanhChinh.LoadThongTinHanhChinh(phieuSieuAmEntity.MaBenhNhan);

            MaDichVu = phieuSieuAmEntity.MaDichVu;
            LoadInfoPhieu(phieuSieuAmEntity);

            ucChiDinhKhoaPhong.LoadDmKhoaKham();
            ucChiDinhKhoaPhong.LoadDmKhoaCanLamSang();
            ucChiDinhKhoaPhong.LoadDmPhongKham();
            ucChiDinhKhoaPhong.LoadDmPhongCanLamSang();

            ucChiDinhKhoaPhong.setMaKhoaKham(phieuSieuAmEntity.MaKhoaKham);
            ucChiDinhKhoaPhong.setMaPhongKhamBenh(phieuSieuAmEntity.MaPhongKham);
            ucChiDinhKhoaPhong.setMaKhoaCanLamSang(phieuSieuAmEntity.MaKhoaCanLamSang);
            ucChiDinhKhoaPhong.setMaPhongCanLamSang(phieuSieuAmEntity.MaPhongCanLamSang);
            ucChiDinhKhoaPhong.LoadDmDichVuChiTiet(MaDichVu);
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            ucChiDinhKhoaPhong.IsValidLookupEdit();
            ucChiDinhKhoaPhong.IsValidChooseDichVuChiTiet();

            if (resultInsert == false)
            {
                phieuSieuAmEntity = new PhieuSieuAmEntity()
                {
                    MaPhieuSieuAm = MaPhieuSieuAm,
                    Stt = 0,
                    SoVaoVien = hoSoKhamBenhNgoaiTruEntity.SoVaoVien,
                    MaHoSo = hoSoKhamBenhNgoaiTruEntity.MaHoSo,
                    NgayVaoKham = hoSoKhamBenhNgoaiTruEntity.NgayVaoKham,
                    YeuCau = ucChiDinhKhoaPhong.getYeuCau(),
                    GhiChu = ucChiDinhKhoaPhong.getGhiChu(),
                    KetQua = txtKetQua.Html.Trim(),
                    KetLuan = txtKetLuan.Html.Trim(),
                    MaBacSyCls = string.Empty,
                    MaBacSyKham = Authenticate.MaBacSy,
                    Anh1 = string.Empty,
                    Anh2 = string.Empty,
                    Anh3 = string.Empty,
                    Anh4 = string.Empty,
                    TrangThaiPhieu = (int)ConstantManagerClinic.TrangThaiPhieuCanLamSang.TaoMoi,
                    MaKhoaCanLamSang = ucChiDinhKhoaPhong.getMaKhoaCanLamSang(),
                    MaPhongCanLamSang = ucChiDinhKhoaPhong.getMaPhongCanLamSang(),
                    MaDichVu = MaDichVu,
                    MaKhoaKham = ucChiDinhKhoaPhong.getMaKhoaKham(),
                    MaPhongKham = ucChiDinhKhoaPhong.getMaPhongKham(),
                    TrangThaiCho = Convert.ToBoolean(ConstantManagerClinic.TrangThaiChoDichVuKhamChuaBenh.ChuaThucHien)
                };

                resultInsert = phieuSieuAmBussiness.InsertPhieuSieuAm(phieuSieuAmEntity);
            }
            else
            {
                phieuSieuAmEntity = phieuSieuAmBussiness.GetPhieuSieuAm(MaPhieuSieuAm);
                phieuSieuAmEntity.YeuCau = ucChiDinhKhoaPhong.getYeuCau();
                phieuSieuAmEntity.GhiChu = ucChiDinhKhoaPhong.getGhiChu();
                phieuSieuAmEntity.KetQua = txtKetQua.Html.Trim();
                phieuSieuAmEntity.KetLuan = txtKetLuan.Html.Trim();
                phieuSieuAmEntity.Anh1 = string.Empty;
                phieuSieuAmEntity.Anh2 = string.Empty;
                phieuSieuAmEntity.Anh3 = string.Empty;
                phieuSieuAmEntity.Anh4 = string.Empty;
                phieuSieuAmEntity.MaKhoaCanLamSang = ucChiDinhKhoaPhong.getMaKhoaCanLamSang();
                phieuSieuAmEntity.MaPhongCanLamSang = ucChiDinhKhoaPhong.getMaPhongCanLamSang();
                phieuSieuAmEntity.MaDichVu = MaDichVu;
                phieuSieuAmEntity.MaKhoaKham = ucChiDinhKhoaPhong.getMaKhoaKham();
                phieuSieuAmEntity.MaPhongKham = ucChiDinhKhoaPhong.getMaPhongKham();

                phieuSieuAmBussiness.EditPhieuSieuAm(phieuSieuAmEntity);
            }

            //delete from table ThuPhiDichVu
            thuPhiDichVuBusiness.DeleteThuPhiDichVuByMaPhieuDichVu(MaPhieuSieuAm);
            //delete from table PhieuSieuAmChiTiet
            phieuSieuAmChiTietBussiness.DeletePhieuSieuAmChiTietByMaPhieuSieuAm(MaPhieuSieuAm);

            if (resultInsert == true)
            {
                //Insert into table ThuPhiDichVu, PhieuSieuAmChiTiet
                IList<DmDichVuChiTietEntity> lstDmDichVuChiTietIsChecked = ucChiDinhKhoaPhong.DmDichVuChiTietEntityIsChecked;
                if (lstDmDichVuChiTietIsChecked == null) { return; }

                foreach (DmDichVuChiTietEntity dmDichVuChiTietEntity in lstDmDichVuChiTietIsChecked)
                {
                    var thuphiDichVuEntity = new ThuPhiDichVuEntity()
                    {
                        MaPhieuDichVu = MaPhieuSieuAm,
                        MaHoSo = phieuSieuAmEntity.MaHoSo,
                        NgayDenKham = phieuSieuAmEntity.NgayVaoKham,
                        MaDichVu = MaDichVu,
                        MaChiTieu = dmDichVuChiTietEntity.MaChiTieu,
                        TongTien = dmDichVuChiTietEntity.DonGia,
                        DaDongTien = (int)ConstantManagerClinic.TrangThaiDongTien.ChuaDongTien,
                        MaNguoiDung = Authenticate.UserName,
                        MaKhoaKham = phieuSieuAmEntity.MaKhoaKham,
                        MaPhongKham = phieuSieuAmEntity.MaPhongKham,
                        MaKhoaCanLamSang = phieuSieuAmEntity.MaKhoaCanLamSang,
                        MaPhongCanLamSang = phieuSieuAmEntity.MaPhongCanLamSang,
                        DaThucHien = Convert.ToBoolean(ConstantManagerClinic.TrangThaiThucHienDichVuKhamChuaBenh.ChuaThucHien)
                    };
                    thuPhiDichVuBusiness.InsertThuPhiDichVu(thuphiDichVuEntity);

                    var phieuSieuAmChiTietEntity = new PhieuSieuAmChiTietEntity()
                    {
                        MaPhieuSieuAm = MaPhieuSieuAm,
                        MaChiTieu = dmDichVuChiTietEntity.MaChiTieu,
                        DonGia = dmDichVuChiTietEntity.DonGia,
                        DaThuTien = (int)ConstantManagerClinic.TrangThaiDongTien.ChuaDongTien
                    };
                    phieuSieuAmChiTietBussiness.InsertPhieuSieuAmChiTiet(phieuSieuAmChiTietEntity);
                }
            }
            if (_SavePhieuCanLamSang != null) _SavePhieuCanLamSang(sender, e);
        }

        void LoadInfoPhieu(PhieuSieuAmEntity obj)
        {
            ucChiDinhKhoaPhong.setMaKhoaKham(obj.MaKhoaKham);
            ucChiDinhKhoaPhong.setMaPhongKhamBenh(obj.MaPhongKham);

            ucChiDinhKhoaPhong.setMaKhoaCanLamSang(obj.MaKhoaCanLamSang);
            ucChiDinhKhoaPhong.setMaPhongCanLamSang(obj.MaPhongCanLamSang);
            ucChiDinhKhoaPhong.setBacSy(obj.MaBacSyKham);
            //txtGhichu.Text = obj.GhiChu;
            txtKetQua.Html = obj.KetQua;
            txtKetLuan.Html = obj.KetLuan;
            txtAnh1.Text = obj.Anh1;
            txtAnh2.Text = obj.Anh2;
            txtAnh3.Text = obj.Anh3;
            txtAnh4.Text = obj.Anh4;

            var dmBacSyClsEntity = dmBacSyBusiness.GetDmBacSy(obj.MaBacSyCls);
            if (dmBacSyClsEntity != null)
                txtBacSyCls.Text = dmBacSyClsEntity.MaBacSy + " - " + dmBacSyClsEntity.TenBacSy;
        }

        #region "Choose Image"
        private void btnAnh1_Click(object sender, EventArgs e)
        {
            txtAnh1.Text = FunctionList.FctOpenFileImage("Chọn hình 1");
        }

        private void btnAnh2_Click(object sender, EventArgs e)
        {
            txtAnh2.Text = FunctionList.FctOpenFileImage("Chọn hình 2");
        }

        private void btnAnh3_Click(object sender, EventArgs e)
        {
            txtAnh3.Text = FunctionList.FctOpenFileImage("Chọn hình 3");
        }

        private void btnAnh4_Click(object sender, EventArgs e)
        {
            txtAnh4.Text = FunctionList.FctOpenFileImage("Chọn hình 4");
        }
        #endregion

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDongKetQua_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuuKetQua_Click(object sender, EventArgs e)
        {
            FunctionList.FctCopyFile(Application.StartupPath, txtAnh1.Text.Trim(), MaPhieuSieuAm + "_Anh1");
            FunctionList.FctCopyFile(Application.StartupPath, txtAnh2.Text.Trim(), MaPhieuSieuAm + "_Anh2");
            FunctionList.FctCopyFile(Application.StartupPath, txtAnh3.Text.Trim(), MaPhieuSieuAm + "_Anh3");
            FunctionList.FctCopyFile(Application.StartupPath, txtAnh4.Text.Trim(), MaPhieuSieuAm + "_Anh4");

            phieuSieuAmEntity = phieuSieuAmBussiness.GetPhieuSieuAm(MaPhieuSieuAm);
            phieuSieuAmEntity.Anh1 = txtAnh1.Text.Trim();
            phieuSieuAmEntity.Anh2 = txtAnh2.Text.Trim();
            phieuSieuAmEntity.Anh3 = txtAnh3.Text.Trim();
            phieuSieuAmEntity.Anh4 = txtAnh4.Text.Trim();
            phieuSieuAmEntity.KetQua = txtKetQua.Html;
            phieuSieuAmEntity.KetLuan = txtKetLuan.Html;
            phieuSieuAmBussiness.EditPhieuSieuAm(phieuSieuAmEntity);
            MessageBox.Show("Cập nhật thành công bản ghi!", ConstantManagerClinic.sTitleInformation, MessageBoxButtons.OK);
        }

        private void frmPhieuSieuAm_Load(object sender, EventArgs e)
        {
            ucChiDinhKhoaPhong.setDmDichVuChiTietEntityIsChecked(MaPhieuSieuAm, MaDichVu);
        }
    }
}